package com.sale.taskplugins;

import com.apache.api.api.TimeTaskService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * 【日终持仓解冻】每在晚上将持仓的冻结加到可用上
 *
 * @author Administrator创建时间：2017-1-12
 */
public class StageTaskPluginImpl implements PluginConnector, TimeTaskService {

    private ProxyManager netProxyManager;

    private IDao trpFeeDao;

    private transient static boolean StageTaskPluginTask = true;

    private Logger log = Logger.getLogger(getClass());

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)
     */
    public Object execute(ParamsVo arg0) throws Exception {
        exeStageTask();
        return new ResultEntity();
    }

    public boolean run() {
        exeStageTask();
        return true;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void exeStageTask() {
        try {
            if (StageTaskPluginTask) {
                StageTaskPluginTask = false;
                String sql =
                        "select goods_id,forzen_Num,use_Num from trs_goods where modify_time like '"
                                + DateUtils.Now.fmt_yyyyMMdd() + "%' ";

                //				String sql = "select * from trs_goods g,trp_deal d where modify_time like '" + DateUtils.Now.fmt_yyyyMMdd() + "%' ";

                MethodParam param = new MethodParam("ByObjInfo", "", "",
                        "com.apache.net.trans.TransPublic");
                param.setParams("dyncSql", sql);
                List<DataMap> objs = trpFeeDao.select(param);
                if (Validator.isEmpty(objs))
                    return;
                int count = objs.size();
                ParamsVo vo = new ParamsVo();
                vo.setKey("iusTransaction");
                vo.setParams("resultType", "processSql");
                vo.setParams("modelTypes", "u_trsGoods");
                for (int i = 0; i < count; i++) {
                    DataMap<String> goods = objs.get(i);
                    try {
                        //
                        vo.setParams("trsGoods.w_goodsId", goods.get("goodsId"));
                        int num = Integer.valueOf(goods.get("useNum")) + Integer
                                .valueOf(StrUtil.doNull(goods.get("forzenNum"), "0"));
                        vo.setParams("trsGoods.useNum", num);
                        vo.setParams("trsGoods.forzenNum", "0");
                        netProxyManager.saveInvoke(vo);
                    } catch (Exception e) {
                        log.error(
                                "执行时间:" + goods.get("modifyTime") + ",用户:" + goods.get("userCname")
                                        + "对产品:" + goods.get("proName") + "解冻失败");
                    }
                }
            }
        } finally {
            StageTaskPluginTask = true;
        }
    }

    public void setNetProxyManager(ProxyManager netProxyManager) {
        this.netProxyManager = netProxyManager;
    }

    public void setTrpFeeDao(IDao trpFeeDao) {
        this.trpFeeDao = trpFeeDao;
    }

}
